package com.example.homework_5_2;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.room.Room;

public class Create_Account_Page extends AppCompatActivity {

    TextView birthday;
    EditText account, password, real_name, id_card;
    Button upload;

    String set_birthday_time = "", account_text = "", password_text = "";
    int id;
    RoodDataBase roodDataBase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_account_page);

        roodDataBase = Room.databaseBuilder(this, RoodDataBase.class, "dataDB").allowMainThreadQueries().build();
        account_text = getIntent().getStringExtra("account_text");
        password_text = getIntent().getStringExtra("password_text");
        id = getIntent().getIntExtra("id", 0);

        birthday = findViewById(R.id.birthday);
        account = findViewById(R.id.account);
        password = findViewById(R.id.password);
        real_name = findViewById(R.id.real_name);
        id_card = findViewById(R.id.id_card);
        upload = findViewById(R.id.upload);

        account.setText(account_text);
        password.setText(password_text);

        birthday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialog1 = new Dialog(v.getContext());
                dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog1.setContentView(R.layout.set_date);

                DatePicker set_date = dialog1.findViewById(R.id.set_date);

                TextView save = dialog1.findViewById(R.id.save);
                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        set_birthday_time = "";
                        if (set_date.getMonth() + 1 < 10 && set_date.getDayOfMonth() < 10) {
                            set_birthday_time += set_date.getYear() + "/" + "0" + (set_date.getMonth() + 1) + "/" + "0" + set_date.getDayOfMonth();
                        }
                        else if (set_date.getMonth() + 1 < 10) {
                            set_birthday_time += set_date.getYear() + "/" + "0" + (set_date.getMonth() + 1) + "/" + set_date.getDayOfMonth();
                        }
                        else if (set_date.getDayOfMonth() < 10) {
                            set_birthday_time += set_date.getYear() + "/" + (set_date.getMonth() + 1) + "/" + "0" + set_date.getDayOfMonth();
                        }
                        else {
                            set_birthday_time += set_date.getYear() + "/" + (set_date.getMonth() + 1) + "/" + set_date.getDayOfMonth();
                        }
                        birthday.setText(set_birthday_time);
                        dialog1.dismiss();
                    }
                });

                dialog1.show();
                dialog1.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, 1500);
                dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog1.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                dialog1.getWindow().setGravity(Gravity.BOTTOM);
            }
        });

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (birthday.getText().toString().equals("请选择日期") || real_name.getText().toString().isEmpty() || id_card.getText().toString().isEmpty()) {
                    Toast.makeText(Create_Account_Page.this, "请输入所有资料", Toast.LENGTH_SHORT).show();
                }
                else {
                    Data data = new Data(real_name.getText().toString(), birthday.getText().toString(), id_card.getText().toString(), password_text, account_text, true);
                    data.setId(id);
                    roodDataBase.dataDao().updateData(data);
                    Intent intent = new Intent(Create_Account_Page.this, First_Page.class);
                    intent.putExtra("account", account_text);
                    intent.putExtra("password", password_text);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }
}